﻿using IBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;
namespace Web612.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RehabilitationController : ControllerBase, IRehabilitationBll
    {
        public RehabilitationController(IRehabilitationBll bll)
        {

            Bll = bll;
        }

        public IRehabilitationBll Bll { get; }

        [HttpPost]
        public Task<int> AddAnync(Rehabilitation rehabilitation)
        {
            return Bll.AddAnync(rehabilitation);
        }
        [HttpGet]
        public List<Rehabilitation> GetRehabilitations()
        {
            return Bll.GetRehabilitations();
        }

        [HttpGet]
        public EmpDto ShowRehabilitation(string? name, int index, int size)
        {
            return Bll.ShowRehabilitation(name, index, size);

        }

        [HttpDelete]
        public Task<int> UpdateRageAnync(string idsting)
        {
            return Bll.UpdateRageAnync(idsting);

        }

        [HttpGet]
        public Task<int> UpdateState(int id)
        {
            return Bll.UpdateState(id);
        }

        [HttpPost]
        public IActionResult ExEcels()
        {
         var she = Bll.GetRehabilitations();
           XSSFWorkbook sheets = new XSSFWorkbook();

            var shee = sheets.CreateSheet("康复信息");

            var row = shee.CreateRow(0);

            row.CreateCell(0).SetCellValue("编号");
            row.CreateCell(1).SetCellValue("项目名称");
            row.CreateCell(2).SetCellValue("时长");
            row.CreateCell(3).SetCellValue("服务数量");
            row.CreateCell(4).SetCellValue("类型编号");
            row.CreateCell(5).SetCellValue("项目说明");
            row.CreateCell(6).SetCellValue("是否删除");
            row.CreateCell(7).SetCellValue("状态");

            int num = 1;
            foreach (var item in she)
            {
                var row1 = shee.CreateRow(num);
                row1.CreateCell(0).SetCellValue(item.Code);
                row1.CreateCell(1).SetCellValue(item.RehabilitationName);
                row1.CreateCell(2).SetCellValue(item.Duration);
                row1.CreateCell(3).SetCellValue(item.Num);
                row1.CreateCell(4).SetCellValue(item.RoteId);
                row1.CreateCell(5).SetCellValue(item.RehabiType);
                row1.CreateCell(6).SetCellValue(item.Isdeletable==true?"未删除":"删除");
                row1.CreateCell(7).SetCellValue(item.IState == true ? "上架" : "下架");
                
                num++;
                
            }

             MemoryStream memory= new MemoryStream();

             sheets.Write(memory);
              byte[] data = memory.ToArray();

            var mimn = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];


            return File(data, mimn, "康复信息.xlsx");







        }
    }
}
